package kr.hkit.androidcomplete03.ETC;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class TestActivity extends Activity {
	EditText edtnum1, edtnum2;
	RadioButton rplus, rminus, rmulti, rdiv;
	Button btnresult;
	int result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_main);
		setTitle("계산기");

		edtnum1 = (EditText) findViewById(R.id.edtNum1);
		edtnum2 = (EditText) findViewById(R.id.edtNum2);

		rplus = (RadioButton) findViewById(R.id.plusRadio);
		rminus = (RadioButton) findViewById(R.id.minusRadio);
		rmulti = (RadioButton) findViewById(R.id.mulRadio);
		rdiv = (RadioButton) findViewById(R.id.divideRadio);

		btnresult = (Button) findViewById(R.id.newwindowopen);

		btnresult.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						SecondActivity.class);
				intent.putExtra("Num1",	Integer.parseInt(edtnum1.getText().toString())); // 인텐트를 생성하고,
				intent.putExtra("Num2", Integer.parseInt(edtnum2.getText().toString())); // 에디트 넘1,넘2에 두 값을 넣었다.

				// 라디오버튼 계산부분
				if (rplus.isChecked()) {
					intent.putExtra("re", "+");
				} else if (rminus.isChecked()) {
					intent.putExtra("re", "-");
				} else if (rmulti.isChecked()) {
					intent.putExtra("re", "*");
				} else if (rdiv.isChecked()) {
					intent.putExtra("re", "/");
				}

				startActivityForResult(intent, 0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) { // setResult()에서 보낸 값이 RESULT_OK면 인텐트에서 돌려받은 값을 토스트로 뿌려줌
			result = data.getIntExtra("result", 0);
			Toast.makeText(getApplicationContext(), "결과 : " + result, Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(getApplicationContext(), "오류입니다", Toast.LENGTH_SHORT).show();
		}
	}

}
